package com.sctu.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.sctu.EMSApp.PalyFoodOneDetailsActivity;
import com.sctu.EMSApp.PlayHotelOneActivity;
import com.sctu.EMSApp.R;
import com.sctu.bean.PlayFoodOneDetailsInfo;
import com.sctu.bean.PlayHotelMoreListBean;
import com.sctu.http.UrlConfig;
import com.sctu.httpvollry.GetVolleyHttp;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 鑫爷您慢点 on 2015/12/17.
 */
public class PlayHotelMoreListAdapter extends BaseAdapter {
    private Activity activity;
    private ViewHolder viewHolder;
    private List<PlayHotelMoreListBean.Results> list = new ArrayList<PlayHotelMoreListBean.Results>();

    public PlayHotelMoreListAdapter(Activity activity, List<PlayHotelMoreListBean.Results> list) {
        this.activity = activity;
        this.list = list;
    }

    public PlayHotelMoreListAdapter() {
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        } else {
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {//优化listView
            view = LayoutInflater.from(activity).inflate(R.layout.item_play_hotel_more,null);
            viewHolder = new ViewHolder();
            viewHolder.setAddressView((TextView) view.findViewById(R.id.item_play_hotel_more_address_text));
            viewHolder.setImageView((ImageView) view.findViewById(R.id.item_play_hotel_more_img));
            viewHolder.setPhoneView((TextView) view.findViewById(R.id.item_play_hotel_more_phone_text));
            viewHolder.setPriceView((TextView) view.findViewById(R.id.item_play_hotel_more_price_text));
            viewHolder.setTitleView((TextView) view.findViewById(R.id.item_play_hotel_more_title_text));
            view.setTag(viewHolder);//将viewHolder储存在view中
        }else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.getTitleView().setText(list.get(position).getTitle());
        viewHolder.getPriceView().setText(list.get(position).getPrice());
        viewHolder.getAddressView().setText(list.get(position).getAddress());
        viewHolder.getPhoneView().setText(list.get(position).getPhone());
        GetVolleyHttp.getInstance(activity).setImageLoader(viewHolder.getImageView(),
                R.drawable.scenerry, R.drawable.error_image, UrlConfig.URL + list.get(position).getPath());
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(activity, PlayHotelOneActivity.class);
                String id = String.valueOf(list.get(position).getId());
                intent.putExtra("id",id);
                activity.startActivity(intent);
            }
        });
        return view;
    }

    /**
     * listView优化内部类
     */
    private class ViewHolder{
        private ImageView imageView;
        private TextView titleView,priceView,addressView,phoneView;

        public ImageView getImageView() {
            return imageView;
        }

        public void setImageView(ImageView imageView) {
            this.imageView = imageView;
        }

        public TextView getTitleView() {
            return titleView;
        }

        public void setTitleView(TextView titleView) {
            this.titleView = titleView;
        }

        public TextView getPriceView() {
            return priceView;
        }

        public void setPriceView(TextView priceView) {
            this.priceView = priceView;
        }

        public TextView getAddressView() {
            return addressView;
        }

        public void setAddressView(TextView addressView) {
            this.addressView = addressView;
        }

        public TextView getPhoneView() {
            return phoneView;
        }

        public void setPhoneView(TextView phoneView) {
            this.phoneView = phoneView;
        }
    }
}
